from typing import Iterable

import scrapy
from scrapy import Request
from lxml import etree

from scrapy_zuqiucaipiao_Dome.items import ScrapyZuqiucaipiaoDomeItem


class ZuqiuSpider(scrapy.Spider):
    name = "zuqiu"
    allowed_domains = ["live.zgzcw.com"]
    # start_urls = ["https://live.zgzcw.com/ls/AllData.action"]

    # def parse(self, response):
    #     pass
    def start_requests(self):
        url = 'https://live.zgzcw.com/ls/AllData.action'
        for i in range(1, 13):
            if i < 10:
                i='0'+str(i)
            for j in range(1,31):
                if j < 10:
                    j='0'+str(j)
                data = {
                    'code': '201',
                    'date': '2024-' + str(i) + '-' + str(j),
                    'ajax': 'true'
                }
                yield scrapy.FormRequest(url=url, formdata=data, callback=self.parse_two)
    def parse_two(self, response):
        tree = etree.HTML(response.text)
        weekday = tree.xpath('//td[1]/em/text()')
        name = tree.xpath('//td[2]/span/text()')
        round_and_grouping = tree.xpath('//td[3]/text()')
        time = tree.xpath('//td[4]/text()')
        status = tree.xpath('//td[5]/strong/text()')
        zhudui = tree.xpath('//td[6]/span/a/text()')
        score = tree.xpath('//td[7]/span/text()')
        kedui = tree.xpath('//span[@class="sptl"]/a/text()')
        jinqiu = tree.xpath('//td[@class="f_sel"]/strong[5]/text()')
        for i in range(len(name)):
            yield ScrapyZuqiucaipiaoDomeItem(weekday=weekday[i],name=name[i], round_and_grouping=round_and_grouping[i], time=time[i], status=status[i], zhudui=zhudui[i], score=score[i], kedui=kedui[i],
                  jinqiu=jinqiu[i])
